package com.jlcindia.dao;

import java.util.List;

import org.hibernate.*;
import org.hibernate.criterion.*;

import com.jlcindia.hibernate.Customer;
import com.jlcindia.util.HibernateTemplate;
import com.jlcindia.util.HibernateUtil;

public class HibernateCustomerDAO implements CustomerDAO {

	@Override
	public int addCustomer(Customer cust) {
		Integer it = (Integer)HibernateTemplate.save(cust);
		return it.intValue();
	}

	@Override
	public void updateCustomer(Customer cust) {
		HibernateTemplate.update(cust);
		
	}

	@Override
	public void deleteCustomer(int cid) {
		HibernateTemplate.delete(Customer.class, cid);
		
	}

	@Override
	public Customer getCustomerByCid(int cid) {
		return (Customer)HibernateTemplate.load(Customer.class, cid);
	}

	@Override
	public Customer getCustomerByEmail(String email) {
		Customer cust = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("email", email));
			cust = (Customer)ct.list().get(0);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return cust;
	}
	

	@Override
	public Customer getCustomerByCardNo(int accNo) {
		Customer cust = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("cardNo", accNo));
			cust = (Customer)ct.list().get(0);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return cust;
	}

	@Override
	public List<Customer> getAllCustomers() {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getAllCustomers(int start, int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
		}

	@Override
	public List<Customer> getCustomersByCity(String city) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("city", city));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByCity(String city, int start, int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("city", city));
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByName(String name) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("cname", name));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByName(String name, int start, int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("cname", name));
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByCardType(String cardType) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("cardType", cardType));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByCardType(String cardType, int start,
			int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("cardType", cardType));
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list; 
	}

	@Override
	public List<Customer> getCustomersByStatus(String status) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("status", status));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByStatus(String status, int start,
			int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("status", status));
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByBal(double bal) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("cardBal", bal));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomersByBal(double bal, int start, int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			ct = ct.add(Restrictions.eq("cardBal", bal));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomers(String city, String cardType) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("city", city)).add(Restrictions.eq("cardType", cardType));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomers(String city, String cardType, int start,
			int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("city", city)).add(Restrictions.eq("cardType", cardType));
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Customer> getCustomers(String city, String cardType,
			String status) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("city", city)).add(Restrictions.eq("cardType", cardType)).add(Restrictions.eq("status", status));
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list; 
	}

	@Override
	public List<Customer> getCustomers(String city, String cardType,
			String status, int start, int total) {
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Criteria ct =session.createCriteria(Customer.class);
			ct = ct.add(Restrictions.eq("city", city)).add(Restrictions.eq("cardType", cardType)).add(Restrictions.eq("status", status));
			ct.setFirstResult(start);
			ct.setMaxResults(total);
			list = ct.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return list; 
	}

}
